﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace App
{
    class Reg
    {
        public static bool AddStartup(string path)
        {
            return true;
        }

        #region AddEntry
        public static void AddEntry(string strPath, int machine, string name, object data)
        {
            RegistryKey reg;

            switch (machine)
            {
                case 0: // Local Machine
                    {
                        try
                        {
                            reg = Registry.LocalMachine.OpenSubKey(strPath, true);
                            reg.SetValue(name, data);
                            reg.Close();
                        }
                        catch (NullReferenceException MyEx)
                        {
                            reg = Registry.LocalMachine.OpenSubKey(strPath, true);
                            reg.CreateSubKey(strPath);
                            reg.Close();

                            MyEx.Message.Trim();

                            AddEntry(strPath, machine, name, data);
                        }
                        reg.Close();
                        break;
                    }

                case 1: // Current User
                    {
                        try
                        {
                            reg = Registry.CurrentUser.OpenSubKey(strPath, true);
                            reg.SetValue(name, data);
                            reg.Close();
                        }
                        catch (NullReferenceException MyEx)
                        {
                            reg = Registry.CurrentUser.OpenSubKey(strPath, true);
                            reg.CreateSubKey(strPath);
                            reg.Close();

                            MyEx.Message.Trim();

                            AddEntry(strPath, machine, name, data);
                        }
                        reg.Close();
                        break;
                    }
            }
        }

        public static void AddEntryRun(string name,string data)
        {
            string str = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
            AddEntry(str, 0, name, data);
        }
        #endregion

        #region IsEntry
        public static bool IsEntry(string strPath,string name,int machine)
        {
            bool isDeleted = false;            
            try
            {
                string[] names;
                RegistryKey key = Registry.LocalMachine.OpenSubKey(strPath); 
                if(machine == 1)
                {
                    key = Registry.Users.OpenSubKey(strPath);
                }
                names = key.GetValueNames();
                foreach (string keyName in names)
                {
                    if (keyName == name)
                    {
                        isDeleted = true;
                        return isDeleted;
                    }
                }
            }
            catch
            { }
            return isDeleted;
        }

        public static bool IsEntryRun(string name)
        {
            string strPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
            return IsEntry(strPath, name,0);
        }
        #endregion

        #region DelEntry
        public static bool DelEntry(string strPath, string name, string fieName, int machine)
        {
            RegistryKey reg;
            switch (machine)
            {
                case 2:
                    System.IO.FileInfo file = new System.IO.FileInfo(fieName);

                    file.Attributes = System.IO.FileAttributes.Normal; // in case if it is readonly
                    file.Delete();

                    return true;

                case 0:
                    reg = Registry.LocalMachine.OpenSubKey(strPath, true);
                    reg.DeleteValue(name, false);
                    reg.Close();
                    return true;

                case 1:
                    reg = Registry.CurrentUser.OpenSubKey(strPath, true);
                    reg.DeleteValue(name, false);
                    reg.Close();
                    return true;
            }

            return false;
        }

        public static bool DelEntryRun(string name)
        {
            string strPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
            return DelEntry(strPath, name, "", 0);
        }
        #endregion

        public static List<Entry> GetEntryList(string strPath,int machine)
        {
            List<Entry> lst = new List<Entry>();
            RegistryKey key = Registry.LocalMachine.OpenSubKey(strPath);
            if (machine == 1)
            {
                key = Registry.Users.OpenSubKey(strPath);
            }
            //
            return lst;
        }
       
    }

    class Entry
    {
        private string m_Name;

        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }
        private object m_Data;

        public object Data
        {
            get { return m_Data; }
            set { m_Data = value; }
        }
        private string m_Path;

        public string Path
        {
            get { return m_Path; }
            set { m_Path = value; }
        }
    }
}
